package com.jxs.android.sxyw;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import org.ksoap2.serialization.SoapObject;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jxs.android.sxyw.boot.SelfApplication;
import com.jxs.android.sxyw.data.NoticeInfo;
import com.jxs.android.sxyw.weiget.PopMsg;
import com.jxs.android.sxyw.weiget.RefreshListView;
import com.jxs.android.sxyw.weiget.TopBar;
import com.jxs.android.util.StartHelper;

/**
 * @Des 巡检列表
 * @Author jiangxs
 * @Des 2012-8-20 下午2:03:35
 */
public class NoticeList extends AbstractListActivity {

	private static final int UPDATE_LIST_UI = 0x403;
	private static final int TO_LOGIN = 0x444;
	private static final int ERROR_MSG = 0x405;
	private static final int REQUEST_TO_DETAIL = 0x404;

	private String ticket = "";

	private RefreshListView refreshListView;
	private TextView empytText;
	private NoticeListInfoAdapter noticeListInfoAdapter;

	private List<NoticeInfo> noticeListInfos = new ArrayList<NoticeInfo>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		initTopBar(savedInstanceState, R.layout.common_with_list);
		refreshListView = (RefreshListView) findViewById(R.id.common_listview);
		empytText = (TextView) findViewById(R.id.null_has_data);
		noticeListInfoAdapter = new NoticeListInfoAdapter(NoticeList.this, noticeListInfos);
		refreshListView.setAdapter(noticeListInfoAdapter);

		ticket = SelfApplication.getUserInfo().getTicket();
		refresh(true);
		refreshListView.setonRefreshListener(new RefreshListView.OnRefreshListener() {
			@Override
			public void onRefresh() {
				refresh(false);
			}
		});
	}

	private void refresh(boolean showDailog) {
		super.getWebServiceData(ticket, 1, Which.COUNT, showDailog, new DataInitListeren() {
			@Override
			public void initData(ProgressDialog progressDialog, boolean isShow) {
				progressDialog.dismiss();
			}
		});
	}

	@Override
	public void topBarLogic(TopBar topBar) {
		topBar.setTitle(R.string.notice_title_label);
		initTopBarReturn(topBar);
		topBar.setLogicBtnText("刷新");
		topBar.setTopBarBtnListern(new TopBar.LogicBtnListern() {
			@Override
			public void onClickLogicBtn() {
				refresh(true);
			}
		});
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void paser(int pageNo, SoapObject soapObject) {
		if (null != soapObject) {
			String stauts = soapObject.getPropertySafelyAsString("status");
			SoapObject msgObjec = (SoapObject) soapObject.getProperty("msg");
			String message = msgObjec.getPropertySafelyAsString("message");
			if ("-2".equals(stauts)) {
				Message msg = new Message();
				msg.what = ERROR_MSG;
				msg.obj = message;
				handler.sendMessage(msg);
			} else if ("-1".equals(stauts)) {
				Message msg = new Message();
				msg.what = TO_LOGIN;
				msg.obj = message;
				handler.sendMessage(msg);
			} else if ("0".equals(stauts)) {
				SoapObject retMsgObject = (SoapObject) soapObject.getProperty("retMsg");
				List<NoticeInfo> noticeListInfos = new ArrayList<NoticeInfo>();
				if (null != retMsgObject) {
					for (int i = 0; i < retMsgObject.getPropertyCount(); i++) {
						SoapObject taskBeanObject = (SoapObject) retMsgObject.getProperty(i);
						NoticeInfo noticeListInfo = new NoticeInfo();
						noticeListInfo.setNotice_id(taskBeanObject.getPropertySafelyAsString("notice_id"));
						noticeListInfo.setNotice_title(taskBeanObject.getPropertySafelyAsString("notice_title"));
						noticeListInfo.setNotice_date(taskBeanObject.getPropertySafelyAsString("notice_date"));
						noticeListInfos.add(noticeListInfo);
					}
				}
				Message msg = new Message();
				msg.what = UPDATE_LIST_UI;
				msg.obj = noticeListInfos;
				handler.sendMessage(msg);
			}
		}
	}

	Handler handler = new Handler() {

		@Override
		@SuppressWarnings("unchecked")
		public void handleMessage(Message msg) {
			if (msg.what == UPDATE_LIST_UI) {
				noticeListInfos.clear();
				noticeListInfos.addAll((List<NoticeInfo>) msg.obj);
				if (noticeListInfos.size() > 0) {
					empytText.setVisibility(View.GONE);
					noticeListInfoAdapter.setNoticeListInfos(noticeListInfos);
					noticeListInfoAdapter.notifyDataSetChanged();
				} else {
					empytText.setVisibility(View.VISIBLE);
				}
				refreshListView.onRefreshComplete();
			} else if (msg.what == ERROR_MSG) {
				PopMsg.show((String) msg.obj);
			} else if (msg.what == TO_LOGIN) {
				PopMsg.show((String) msg.obj);
				StartHelper.startActivity(Login.class);
			}
		}

	};

	private class NoticeListInfoAdapter extends BaseAdapter {
		private List<NoticeInfo> noticeListInfos;
		private AbstractActivity context;
		private WeakReference<AbstractActivity> revokeActivity;

		private LayoutInflater inflater = null;

		public void setNoticeListInfos(List<NoticeInfo> contacts) {
			this.noticeListInfos = contacts;
		}

		public NoticeListInfoAdapter(AbstractActivity revokeActivity, List<NoticeInfo> laucherInfos) {
			init(revokeActivity);
			this.noticeListInfos = laucherInfos;
		}

		private void init(AbstractActivity revokeActivity) {
			this.revokeActivity = new WeakReference<AbstractActivity>(revokeActivity);
			context = this.revokeActivity.get();
			inflater = (LayoutInflater) revokeActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		public int getCount() {
			return noticeListInfos.size();
		}

		public NoticeInfo getItem(int position) {
			return noticeListInfos.get(position);
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			final NoticeInfo noticeListInfo = noticeListInfos.get(position);
			ViewHolder holder = null;
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.list_item, null);
				holder = new ViewHolder();
				holder.item_icon = (ImageView) convertView.findViewById(R.id.item_icon);
				holder.title_tv = (TextView) convertView.findViewById(R.id.item_title);
				holder.desc_tv = (TextView) convertView.findViewById(R.id.item_des);
				holder.list_menu = (ImageView) convertView.findViewById(R.id.item_arrow);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.item_icon.setImageResource(R.drawable.icon_notice);
			holder.title_tv.setText(noticeListInfo.getNotice_title());
			holder.desc_tv.setText(noticeListInfo.getNotice_date());

			OnClickListener onClickListener = new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					onclickView(noticeListInfo);
				}
			};

			holder.list_menu.setOnClickListener(onClickListener);
			convertView.setOnClickListener(onClickListener);

			if (position % 2 == 0) {
				convertView.setBackgroundResource(R.drawable.roundlist_body_gray);
			} else {
				convertView.setBackgroundResource(R.drawable.roundlist_body);
			}

			return convertView;
		}

		private void onclickView(NoticeInfo noticeListInfo) {
			Intent intent = new Intent(context, NoticeDetail.class);
			Bundle bundle = new Bundle();
			bundle.putSerializable("noticeInfo", noticeListInfo);
			intent.putExtras(bundle);
			context.startActivityForResult(intent, REQUEST_TO_DETAIL);
		}

		class ViewHolder {
			TextView title_tv;//
			TextView desc_tv;//
			ImageView list_menu;//
			ImageView item_icon;//
		}

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == REQUEST_TO_DETAIL) {
			if (resultCode == RESULT_OK) {

			}
		}

	}

	@Override
	public String getMethod() {
		return "getNoticeList";
	}

}
